import poser, string, os
os.chdir(os.path.dirname(poser.AppLocation()))

class PzrRect(object):
	def __init__(self, id=1, left=43, top=96, right=53, bottom=106):
		self.id = id
		self.top = top
		self.left = left
		self.right = right
		self.bottom = bottom

class PzrText(object):
	def __init__(self, color=-16777216, size=14, align=0, font=0, italic=0, antialiased=1, offsetX=0, offsetY=0):
		self.color = color
		self.size = size
		self.align = align
		self.font = font
		self.italic = italic
		self.antialiased = antialiased
		self.offsetX = offsetX
		self.offsetY = offsetY

class PzrPillow(object):
	def __init__(self, left=-1, middle=-1, right=-1, lbtn=26020, mbtn=26019, rbtn=26018):
		self.left = left
		self.middle = middle
		self.right = right
		self.lbtn = lbtn
		self.mbtn = mbtn
		self.rbtn = rbtn

def FindHex(i):
	h = hex(i)[2:]
	if len(h) < 2: h = "0" + h
	return h

def PzrColor(R,G,B):
	if R > 255: raise "Red value exceeded 255."
	if G > 255: raise "Green value exceeded 255."
	if B > 255: raise "Blue value exceeded 255."
	rgb = "0x00" + FindHex(R) + FindHex(G) + FindHex(B)
	return string.atoi(rgb,16)

class PzrXMLTag(object):
	def __init__(self, type="Widget", opener="<", closer="/>", spacer=" "):
		self.type = type
		self.closer = closer
		self.spacer = spacer
		self.string = opener + type + spacer
	def addValue(self, arg, value):
		if value == "none": return None
		if type(arg) != type("string"):
			arg = repr(arg)
		if type(value) != type("string"):
			value = repr(value)
		self.string = self.string + arg + '="' + value + '"' + self.spacer
	def close(self):
		self.string = self.string + self.closer
		return self.string
	def get(self):
		return self.string

class PzrWidgetList(object):
	def __init__(self, file, PSDFiles=["./runtime/ui/26000_utility.psd"], useBorders=1, margin_left=20, margin_top=30, margin_right=20, margin_bottom=30):
		self.file = file
		self.path = self.formatPath(self.file)
		self.xml = """<?xml version="1.0"?>"""
		self.addXML( self.addTag("WidgetList", {"XmlFile":self.path}, closer=">"), indent=0)
		for PSD in PSDFiles:
			self.addXML( self.addTag("PSDFile", {"name":PSD}), indent=1 )
		self.addXML( PzrBackground("Background").place(-10,-10), indent=1)
		if useBorders:
			self.addXML( PzrTopLeftBorder(width=margin_left, height=margin_top).place(0,0), indent=1)
			self.addXML( PzrTopCenterBorder(width=margin_left, height=margin_top).place(0,0), indent=1)
			self.addXML( PzrTopRightBorder(width=margin_right, height=margin_top).place(0,0), indent=1)
			self.addXML( PzrLeftBorder(width=margin_left, height=margin_top).place(0,0), indent=1)
			self.addXML( PzrRightBorder(width=margin_right, height=margin_top).place(0,0), indent=1)
			self.addXML( PzrBottomLeftBorder(width=margin_left, height=margin_bottom).place(0,0), indent=1)
			self.addXML( PzrBottomCenterBorder(width=margin_left, height=margin_bottom).place(0,0), indent=1)
			self.addXML( PzrBottomRightBorder(width=margin_right, height=margin_bottom).place(0,0), indent=1)
	def addXML(self, tag, indent=0):
		txt = self.xml + "\n\n"
		for i in range(indent):
			txt = txt + "\t"
		txt = txt + tag
		self.xml = txt
	def addTag(self, type, values, opener="<", closer="/>"):
		tag = PzrXMLTag(type=type, opener=opener, closer=closer)
		for key in values.keys():
			tag.addValue(key, values[key])
		tag.close()
		return tag.get()
	def close(self):
		self.addXML( self.addTag("StringResIDs", {"major":0}), indent=1)
		self.addXML( self.addTag("WidgetList", {}, opener="</",closer=">"), indent=0)
		self.writeXML(self.file, self.xml)
		return self.xml
	def formatPath(self, path):
		path = string.join(string.split(path,"\\"),"/")
		path = string.join(string.split(path,":"),"/")
		if path[0] == "/": path = path[1:]
		return "./" + path
	def writeXML(self, path, data):
		fi = open(path,'w')
		fi.write(data)
		fi.close()
		return path

class PzrGUIObject(object):
        def __init__(self):
                pass

class PzrWidget(PzrGUIObject):
	def configure(self, value=-1, callback=-1, title=-1):
		if value != -1:
                        self.buttonValue = value
		if callback != -1:
                        self.callback = callback
		if title != -1:
                        self.title = title
	def AddButtonCallback(self, dialog, callback):
		dialog.AddButtonCallback(self.widgetName, callback=callback)
	def SetButtonValue(self, dialog, value):
		dialog.SetButtonValue(self.widgetName, value=value)
	def SetText(self, dialog, message):
		dialog.SetText(self.widgetName, text=message)
	def SetDialog(self, dialog):
                self.dialog = dialog
		dialog.SetText(self.widgetName, text=self.title)
		dialog.SetButtonValue(self.widgetName, value=self.buttonValue)
		if self.callback:
                        dialog.AddButtonCallback(self.widgetName, callback=self.callback)
	def __init__(self, widgetName, callback=None, height=0, width=0, buttonValue=0, command="none", groupID="none", pictRes="none", hiliteRes="none", behaviorProcID="none", preliteAction="none", bb="none", persistBetweenRooms="none", rect="none", useRectClick="none", pin="none", color="none", fadeMax="none", tile="none", isStringID="none", text="none", padding="none", pillow="none", widgetText="none", useAlpha="none", widgetProcID="none"):
		self.title = widgetName
		self.callback = callback
		self.buttonValue = buttonValue
		self.widgetName = widgetName
		self.command = command
		self.groupID = groupID
		self.pictRes = pictRes
		self.hiliteRes = hiliteRes
		self.behaviorProcID = behaviorProcID
		self.preliteAction = preliteAction
		self.bb = bb
		self.persistBetweenRooms = persistBetweenRooms
		self.rect = rect
		self.useRectClick = useRectClick
		self.pin = pin
		self.color = color
		self.fadeMax = fadeMax
		self.tile = tile
		self.isStringID = isStringID
		self.text = text
		self.padding = padding
		self.pillow = pillow
		self.widgetText = widgetText
		self.useAlpha = useAlpha
		self.widgetProcID = widgetProcID
		self.height = height
		self.width = width
		if height and width:
			self.bb = PzrRect(left=0,top=0,bottom=height,right=width)
			self.rect = PzrRect(left=0,top=0,bottom=height,right=width)
	def place(self, x, y):
		self.xml = PzrXMLTag(type="Widget")
		self.xml.addValue("name", self.widgetName)
		self.xml.addValue("command", self.command)
		self.xml.addValue("groupID", self.groupID)
		self.xml.addValue("pictRes", self.pictRes)
		self.xml.addValue("hiliteRes", self.hiliteRes)
		self.xml.addValue("behaviorProcID", self.behaviorProcID)
		self.xml.addValue("widgetProcID", self.widgetProcID)
		self.xml.addValue("preliteAction", self.preliteAction)
		if self.bb != "none":
			self.xml.addValue("left",self.bb.left + x)
			self.xml.addValue("top", self.bb.top + y)
			self.xml.addValue("right", self.bb.right + x)
			self.xml.addValue("bottom", self.bb.bottom + y)
		self.xml.addValue("persistBetweenRooms", self.persistBetweenRooms)
		self.xml.addValue("useRectClick", self.useRectClick)
		if self.rect != "none":
			self.xml.addValue("rect.left", self.rect.left + x)
			self.xml.addValue("rect.top", self.rect.top + y)
			self.xml.addValue("rect.right", self.rect.right + x)
			self.xml.addValue("rect.bottom", self.rect.bottom + y)
		if self.pin != "none":
			self.xml.addValue("pinID", self.pin.id)
			self.xml.addValue("pinTop", self.pin.top)
			self.xml.addValue("pinLeft", self.pin.left)
			self.xml.addValue("pinBottom", self.pin.bottom)
			self.xml.addValue("pinRight", self.pin.right)
		self.xml.addValue("color", self.color)
		self.xml.addValue("fadeMax", self.fadeMax)
		self.xml.addValue("tile", self.tile)
		self.xml.addValue("isStringID", self.isStringID)
		if self.text != "none":
			self.xml.addValue("textColor", self.text.color)
			self.xml.addValue("textSize", self.text.size)
			self.xml.addValue("textAlign", self.text.align)
			self.xml.addValue("textFont", self.text.font)
			self.xml.addValue("textItalic", self.text.italic)
			self.xml.addValue("textAntialiased", self.text.antialiased)
			self.xml.addValue("textOffsetX", self.text.offsetX)
			self.xml.addValue("textOffsetY", self.text.offsetY)
		self.xml.addValue("padding", self.padding)
		if self.pillow != "none":
			self.xml.addValue("leftPillowID", self.pillow.left)
			self.xml.addValue("middlePillowID", self.pillow.middle)
			self.xml.addValue("rightPillowID", self.pillow.right)
			self.xml.addValue("leftButtonID", self.pillow.lbtn)
			self.xml.addValue("middleButtonID", self.pillow.mbtn)
			self.xml.addValue("rightButtonID", self.pillow.rbtn)
		self.xml.addValue("widgetText", self.widgetText)
		self.xml.addValue("useAlpha", self.useAlpha)
		self.xml.close()
		return self.xml.get()

class PzrBackground(PzrWidget):
        def __init__(self, widgetName, command=0, pictRes=26076, behaviorProcID=4, bb=PzrRect(left=0, top=0, right=26, bottom=26), persistBetweenRooms=1, rect=PzrRect(left=0, top=0, right=480, bottom=400), pin=PzrRect(id=1, left=1, top=3, right=2, bottom=4), color=-1, fadeMax=0, tile=1):
                PzrWidget.__init__(self, widgetName, command=command, pictRes=pictRes, behaviorProcID=4, bb=bb, persistBetweenRooms=1, rect=rect, pin=pin, color=color, fadeMax=fadeMax, tile=tile)

class PzrBorder(PzrWidget):
        def __init__(self, widgetName,  command=0,  pictRes=26084,  behaviorProcID=4,  pin=PzrRect(id=1,left=1,top=3,right=1,bottom=3),  bb=PzrRect(left=0,top=0,bottom=30,right=20),  persistBetweenRooms=1, rect=PzrRect(left=0,top=0,right=20,bottom=30),  color=-1,  fadeMax=0, tile="none"):
                PzrWidget.__init__(self, widgetName,  command=command,  pictRes=pictRes,  behaviorProcID=behaviorProcID,  pin=pin,  bb=bb,  persistBetweenRooms=persistBetweenRooms, rect=rect,  color=color,  fadeMax=fadeMax, tile=tile)

class PzrTopLeftBorder(PzrBorder):
        def __init__(self, width=20, height=30):
                PzrBorder.__init__(self, "topLeftDlgBorder",  pictRes=26084,  pin=PzrRect(id=1,left=1,top=3,right=1,bottom=3),  bb=PzrRect(left=0,top=0,bottom=height,right=width), rect=PzrRect(left=0,top=0,right=20,bottom=30))

class PzrTopCenterBorder(PzrBorder):
        def __init__(self, width=20, height=30):
                PzrBorder.__init__(self, "topCenterDlgBorder",  pictRes=26083,  pin=PzrRect(id=1,left=1,top=3,right=2,bottom=3),  bb=PzrRect(left=width,top=0,bottom=height-3,right=width+40), rect=PzrRect(left=20,top=0,right=560,bottom=27), tile=1)

class PzrTopRightBorder(PzrBorder):
        def __init__(self, width=20, height=30):
                PzrBorder.__init__(self, "topRightDlgBorder",  pictRes=26082,  pin=PzrRect(id=1,left=2,top=3,right=2,bottom=3),  bb=PzrRect(left=-1*width,top=0,bottom=height,right=0), rect=PzrRect(left=-20,top=0,right=0,bottom=30))

class PzrLeftBorder(PzrBorder):
        def __init__(self, width=20, height=30):
                PzrBorder.__init__(self, "leftDlgBorder",  pictRes=26081,  pin=PzrRect(id=1,left=1,top=3,right=1,bottom=4),  bb=PzrRect(left=0,top=height,bottom=-1*height,right=width-6), rect=PzrRect(left=0,top=80,right=14,bottom=420), tile=1)

class PzrRightBorder(PzrBorder):
        def __init__(self, width=20, height=30):
                PzrBorder.__init__(self, "rightDlgBorder",  pictRes=26080,  pin=PzrRect(id=1,left=2,top=3,right=2,bottom=4),  bb=PzrRect(left=-1*width+6,top=height,bottom=-1*height,right=0), rect=PzrRect(left=486,top=80,right=500,bottom=420), tile=1)

class PzrBottomLeftBorder(PzrBorder):
        def __init__(self, width=20, height=30):
                PzrBorder.__init__(self, "bottomLeftDlgBorder",  pictRes=26079,  pin=PzrRect(id=1,left=1,top=4,right=1,bottom=4),  bb=PzrRect(left=0,top=-1*height-10,bottom=0,right=width), rect=PzrRect(left=0,top=410,right=20,bottom=450))

class PzrBottomCenterBorder(PzrBorder):
        def __init__(self, width=20, height=30):
                PzrBorder.__init__(self, "bottomMiddleDlgBorder",  pictRes=26078,  pin=PzrRect(id=1,left=1,top=4,right=2,bottom=4),  bb=PzrRect(left=width,top=-1*height-5,bottom=0,right=-1*width), rect=PzrRect(left=20,top=415,right=480,bottom=450), tile=1)

class PzrBottomRightBorder(PzrBorder):
        def __init__(self, width=20, height=30):
                PzrBorder.__init__(self, "bottomRightDlgBorder",  pictRes=26077,  pin=PzrRect(id=1,left=2,top=4,right=2,bottom=4),  bb=PzrRect(left=-1*width,top=-1*height-10,bottom=0,right=0), rect=PzrRect(left=480,top=410,right=500,bottom=450))

class PzrRadiobutton(PzrWidget):
        def __init__(self, widgetName, command=10002, groupID=23, pictRes=26072, hiliteRes=26071, behaviorProcID=3, preliteAction=1, bb=PzrRect(left=0,top=0,right=10,bottom=10), rect=PzrRect(left=0,top=0,right=10,bottom=10), useRectClick=1, pin=PzrRect(id=1,left=1,top=3,right=1,bottom=3), color=-1, fadeMax=0):
                PzrWidget.__init__(self, widgetName, command=command, groupID=groupID, pictRes=pictRes, hiliteRes=hiliteRes, behaviorProcID=behaviorProcID, preliteAction=preliteAction, bb=bb, rect=rect, useRectClick=useRectClick, pin=pin, color=color, fadeMax=fadeMax)

class PzrCheckbutton(PzrWidget):
        def __init__(self, widgetName, command=10002, groupID=23, pictRes=26105, hiliteRes=26104, behaviorProcID=3, preliteAction=1, bb=PzrRect(left=0,top=0,right=17,bottom=18), rect=PzrRect(left=0,top=0,right=17,bottom=18), useRectClick=1, pin=PzrRect(id=1,left=1,top=3,right=1,bottom=3), color=-1, fadeMax=0):
        	PzrWidget.__init__(self, widgetName, command=command, groupID=groupID, pictRes=pictRes, hiliteRes=hiliteRes, behaviorProcID=behaviorProcID, preliteAction=preliteAction, bb=bb, rect=rect, useRectClick=useRectClick, pin=pin, color=color, fadeMax=fadeMax)

class PzrMessage(PzrWidget):
        def __init__(self, widgetName, command=0, groupID=23, widgetProcID=3, height=0, width=0, widgetText="0:2", isStringID=1, text=PzrText(), preliteAction=6, pin=PzrRect(id=1,top=3,left=1,bottom=3,right=1), rect=PzrRect(left=0,top=0,right=640,bottom=21), bb=PzrRect(left=0,top=0,right=640,bottom=21), color=-1, fadeMax=0, behaviorProcID="none"):
                PzrWidget.__init__(self, widgetName, command=command, groupID=groupID, widgetProcID=widgetProcID, height=height, width=width, widgetText=widgetText, isStringID=isStringID, text=text, preliteAction=preliteAction, pin=pin, rect=rect, bb=bb, color=color, fadeMax=fadeMax, behaviorProcID=behaviorProcID)

class PzrButtonWidget(PzrWidget):
        def __init__(self, widgetName, callback=None, command=10001, groupID=4, pictRes=26019, widgetProcID=23, height=0, width=0, padding=12, pillow=PzrPillow(), widgetText="0:2", isStringID=1, text=PzrText(color=-16777216,size=16,align=0), behaviorProcID=1, useAlpha=0, preliteAction=1, pin=PzrRect(id=1,top=3,left=1,bottom=3,right=1), bb=PzrRect(left=0,top=0,right=60,bottom=20), persistBetweenRooms=1, rect=PzrRect(left=0,top=0,right=60,bottom=20), color=-16777216, fadeMax=0):
        	PzrWidget.__init__(self, widgetName, callback=callback, command=command, groupID=4, pictRes=pictRes, widgetProcID=widgetProcID, height=height, width=width, padding=padding, pillow=pillow, widgetText=widgetText, isStringID=isStringID, text=text, behaviorProcID=behaviorProcID, useAlpha=useAlpha, preliteAction=preliteAction, pin=pin, bb=bb, persistBetweenRooms=persistBetweenRooms, rect=rect, color=color, fadeMax=fadeMax)

class PzrImage(PzrWidget):
        def __init__(self, widgetName, command=0, pictRes=26084, hiliteRes="none", behaviorProcID=4, pin=PzrRect(id=1,left=1,top=3,right=1,bottom=3), height=0, width=0, bb=PzrRect(left=0,top=0,bottom=64,right=64), persistBetweenRooms=1, rect=PzrRect(left=0,top=0,right=64,bottom=64),  color=-1,  fadeMax=0, tile="none"):
                PzrWidget.__init__(self, widgetName, command=command, pictRes=pictRes, hiliteRes=hiliteRes, behaviorProcID=behaviorProcID, pin=pin, bb=bb, height=height, width=width, persistBetweenRooms=persistBetweenRooms, rect=bb,  color=color,  fadeMax=fadeMax, tile=tile)

class PzrImageButton(PzrWidget):
        def __init__(self, widgetName, callback=None, command=10002, groupID=23, pictRes=26105, hiliteRes=26104, width=0, height=0, behaviorProcID=1, preliteAction=1, useRectClick=1, pin=PzrRect(id=1,left=1,top=3,right=1,bottom=3), color=-1, fadeMax=0):
                PzrWidget.__init__(self, widgetName, callback=callback, command=command, groupID=groupID, pictRes=pictRes, hiliteRes=hiliteRes, width=width, height=height, behaviorProcID=behaviorProcID, preliteAction=preliteAction, pin=pin, color=color, fadeMax=fadeMax )

class PzrLabeledWidget(PzrGUIObject):
        def __init__(self, widgetName, ButtonClass, *args, **kwds):
                self.widgetName = widgetName
                for slot, value in kwds.items():
                        self.__setattr__(slot, value)
                self.btn = ButtonClass(self.widgetName, command=self.command, groupID=self.groupID)
                if ButtonClass == PzrCheckbutton:
                        bounding = PzrRect(left=12, top=-8, right=self.width-10, bottom=self.height-10)
                else:
                        bounding = PzrRect(left=6, top=-12, right=self.width-10, bottom=self.height-10)
                self.msg = PzrMessage(self.widgetName + "Message", command=self.command, groupID=self.groupID, bb=bounding, text=self.text)
        def place(self, x, y):
                return "\t" + self.btn.place(x, y) + "\n\t" + self.msg.place(x+10, y+10)
        def toggle(self, dialog):
                return None
        def buttonFunction(self):
                if self.callback:
                        return self.callback
                else:
                        return lambda v, f=self.toggle, d=self.dialog: f(d)
        def SetText(self, dialog, message):
                self.msg.SetText(dialog, message)
        def SetDialog(self, dialog):
                self.dialog = dialog
                self.SetText(dialog, self.widgetName)
                dialog.AddButtonCallback(self.widgetName, callback=self.buttonFunction())
                dialog.AddButtonCallback(self.msg.widgetName, callback=self.buttonFunction())

class PzrRadio(PzrLabeledWidget):
        def __init__(self, widgetName, callback=None, command=10002, groupID=23, width=126, height=21, text="none"):
                PzrLabeledWidget.__init__(self, widgetName, PzrRadiobutton, callback=callback, command=command, groupID=groupID, width=width, height=height, text=text)

class PzrCheck(PzrLabeledWidget):
        def __init__(self, widgetName, initOn=0, callback=None, command=10002, groupID=23, width=126, height=21, text="none"):
                PzrLabeledWidget.__init__(self, widgetName, PzrCheckbutton, callback=callback, command=command, groupID=groupID, width=width, height=height, text=text)
                self.var = initOn
	def get(self):
		return self.var
	def toggle(self, dialog):
		if not self.var: self.select(dialog)
		else: self.deselect(dialog)
	def select(self, dialog):
		self.var = 1
		dialog.SetButtonValue(name=self.widgetName, value=1)
	def deselect(self, dialog):
		self.var = 0
		dialog.SetButtonValue(name=self.widgetName, value=0)

class PzrTextArea(PzrGUIObject):
        def __init__(self, widgetName, text=PzrText(), message=""):
                self.widgetName = widgetName
                self.text = text
                self.messages = []
                self.message = message
                lines = message.split("\n")
                for line in lines:
                        self.addMessage(line)
        def addMessage(self, text):
                msg = PzrMessage(self.widgetName + repr(len(self.messages)), text=self.text)
                msg.configure(title=text)
                self.messages.append(msg)
        def SetText(self, dialog, message):
                self.message = message
                lines = message.split("\n")
                i = 0
                for msg in self.messages:
                        if i >= len(lines):
                                msg.SetText(dialog, "")
                        else:
                                msg.SetText(dialog, lines[i])
                        i = i + 1
        def SetDialog(self, dialog):
                self.dialog = dialog
                for msg in self.messages:
                        msg.SetDialog(dialog)
        def place(self, x, y):
                self.xml = ""
                curx = x
                cury = y
                import math
                i = math.floor(self.text.size * 1.2)
                for message in self.messages:
                        self.xml = self.xml + "\n" + message.place(curx, cury)
                        cury = cury + i
                return self.xml

class PzrButtonPane(PzrGUIObject):
	def __init__(self, groupID=23, initCommand=10002, itemsPerCol=13, valueMap=None, initItem=0, text="none"):
		self.buttons = []
		self.var = initItem
		self.valueMap = valueMap
		self.selected = []
		self.initCommand = initCommand
		self.curCommand = initCommand
		self.groupID = groupID
		self.itemsPerCol = itemsPerCol
		self.lookup_table = {}
		self.padding = {}
		self.dlg = None
		self.text = text
	def AddButton(self, button):
		self.buttons.append(button)
		self.lookup_table[button.widgetName] = len(self.buttons)-1
		return button
	def AddImageRadio(self, widgetName, width=109, height=108, pictRes=0, hiliteRes=0):
                self.curCommand = self.curCommand + 1
                return self.AddButton( PzrImageButton(widgetName, command=self.curCommand, groupID=self.groupID, width=width, height=height, pictRes=pictRes, hiliteRes=hiliteRes) )
	def AddCheckbutton(self, widgetName, width=126, height=21):
		command = self.curCommand
		self.curCommand = command + 1
		return self.AddButton( PzrCheck(widgetName, command=command, groupID=self.groupID, width=width, height=height, text=self.text) )
	def AddRadiobutton(self, widgetName):
		command = self.curCommand
		self.curCommand = command + 1
		button = self.AddButton( PzrRadio(widgetName, command=command, groupID=self.groupID, text=self.text) )
		self.var = self.buttons[0].widgetName
		return button
	def AddPaddingAfter(self, widgetName, padX, padY):
		self.padding[widgetName] = (padX, padY)
	def place(self, x, y):
		self.xml = ""
		curx = x
		cury = y
		i = 1
		for button in self.buttons:
			self.xml = self.xml + "\n" + button.place(curx, cury)
			if self.padding.has_key(button.widgetName):
				x = x + self.padding[button.widgetName][0]
				y = y + self.padding[button.widgetName][1]
			curx = x + int(i / self.itemsPerCol) * button.width
			cury = y + int(i % self.itemsPerCol) * button.height
			i = i + 1
		return self.xml
	def get(self):
		return self.var
	def set(self, value):
		self.var = value
	def onClick(self, v):
		pass
	def setOnClick(self, callback):
		self.onClick = lambda v: callback(v)
	def SetText(self, dialog):
		for button in self.buttons:
			button.SetText(dialog, button.widgetName)
	def SetValues(self, dialog):
		for button in self.buttons:
			dialog.AddButtonCallback(name=button.widgetName, callback=lambda v, s=self, wN=button.widgetName: s.set(wN))
			dialog.SetButtonValue(name=button.widgetName, value=0)
		dialog.SetButtonValue(self.buttons[0].widgetName, value=1)
	def SetDialog(self, dialog):
		self.dlg = dialog
		self.SetText(dialog)
		self.SetValues(dialog)

class PzrRadioPane(PzrButtonPane):
        def __init__(self, groupID=23, initCommand=10002, itemsPerCol=13, initItem=0, text="none"):
                PzrButtonPane.__init__(self, groupID=groupID, initCommand=initCommand, itemsPerCol=itemsPerCol, initItem=initItem, text=text)
        def get(self):
                return self.var
        def set(self, value):
                self.var = value
                if self.dlg:
                        for button in self.buttons:
                                if button.widgetName == value:
                                        continue
                                else:
                                        self.dlg.SetButtonValue(name=button.widgetName, value=0)
                        self.dlg.SetButtonValue(name=value, value=1)
                        self.onClick(value)
	def SetDialog(self, dialog):
                PzrButtonPane.SetDialog(self, dialog)
		self.set(self.var)

class PzrCheckPane(PzrButtonPane):
        def __init__(self, groupID=23, initCommand=10002, itemsPerCol=13, valueMap=None, text="none"):
                PzrButtonPane.__init__(self, groupID=groupID, initCommand=initCommand, itemsPerCol=itemsPerCol, valueMap=valueMap, text=text)
	def get(self):
		return self.selected
	def select(self, dialog, widgetName):
		if not widgetName in self.selected:
			self.selected.append(widgetName)
		dialog.SetButtonValue(name=widgetName, value=1)
	def deselect(self, dialog, widgetName):
		if widgetName in self.selected:
			self.selected.remove(widgetName)
		dialog.SetButtonValue(name=widgetName, value=0)
	def set(self, dialog, value):
		if value in self.selected:
			self.selected.remove(value)
			if self.dlg:
        			dialog.SetButtonValue(name=value, value=0)
		else:
			self.selected.append(value)
			if self.dlg:
        			dialog.SetButtonValue(name=value, value=1)
	def SetValues(self, dialog):
		if not self.valueMap:
                        self.valueMap = [0] * len(self.buttons)
		i = 0
		for button in self.buttons:
			dialog.AddButtonCallback(name=button.widgetName, callback=lambda v, s=self, d=dialog, n=button.widgetName: s.set(d,n))
			if self.valueMap[i]:
                                self.select(dialog, button.widgetName)
			else:
                                self.deselect(dialog, button.widgetName)
			i = i + 1

class PzrOkButton(PzrWidget):
        def __init__(self):
                PzrWidget.__init__(self, "okBtn",  command=7001,  groupID=4,  pictRes=26016,  behaviorProcID=1,  preliteAction=1,  pin=PzrRect(id=1,top=3,left=1,bottom=3,right=1),  bb=PzrRect(left=0,top=0,bottom=19,right=58),  persistBetweenRooms=1,  rect=PzrRect(left=0,top=0,right=58,bottom=19),  color=-1,  fadeMax=0)

class PzrCancelButton(PzrWidget):
        def __init__(self):
                PzrWidget.__init__(self, "cancelBtn",  command=7002,  groupID=4,  pictRes=26017,  behaviorProcID=1,  preliteAction=1,  pin=PzrRect(id=1,top=3,left=1,bottom=3,right=1),  bb=PzrRect(left=0,top=0,bottom=19,right=58),  persistBetweenRooms=1,  rect=PzrRect(left=0,top=0,right=58,bottom=19),  color=-1,  fadeMax=0)

class PzrButton(PzrButtonWidget):
	def __init__(self, widgetName, callback=None, command=10002, groupID=4):
		PzrButtonWidget.__init__(self, widgetName, command=command, groupID=groupID, callback=callback)
	def changeText(self, newText):
                if self.dialog:
                        PzrButtonWidget.SetText(self, self.dialog, newText)
	def SetDialog(self, dialog, function=None):
                self.dialog = dialog
		self.SetText(dialog)
		if function:
                        self.callback = function
		self.AddButtonCallback(dialog, self.callback)
	def SetText(self, dialog):
                PzrButtonWidget.SetText(self, dialog, self.widgetName)

class PzrValueButton(PzrButton):
        def __init__(self, widgetName, command=10002, groupID=4, text="!NULL", value="!NULL", message=None, options=None, suffix=""):
                PzrButton.__init__(self, widgetName, command=command, groupID=groupID)
                self.suffix = suffix
                self.setDefaults()
                if value != self.null:
                        self.default = value
                else:
                        self.default = None
                if message:
                        self.message = message
                else:
                        self.message = "Pick value."
                if options:
                        self.options = options
                else:
                        self.options = []
                if text != "!NULL":
                        self.title = self.FormatText(text)
                else:
                        self.title = "Load"
                self.value = self.default
        def setDefaults(self):
                self.null = "!NULL"
                self.defaultOptions = []
                self.defaultMessage = "Pick value."
                self.defaultValue = None
                self.defaultText = "Load"
	def get(self):
		return self.FormatValue(self.value)
	def set(self, text, value):
		self.ChangeText(text)
		self.ChangeValue(value)
	def Set(self, value):
                self.ChangeText(value)
                self.ChangeValue(value)
	def OnClick(self, event=None):
		newValue = self.AskValue()
		if newValue != self.null:
			self.ChangeValue(newValue)
			self.ChangeText(newValue)
	def AskValue(self):
		return self.null
	def FormatText(self, text):
		return text + self.suffix
	def FormatValue(self, value):
		return value
	def ChangeText(self, text):
		text = self.FormatText(text)
		self.title = text
		try:
                        self.SetText(self.dlg)
		except:
                        pass
	def ChangeValue(self, value):
		self.value = self.FormatValue(value)
	def SetDialog(self, dialog, function=None):
		self.dlg = dialog
		self.SetText(dialog)
		self.AddButtonCallback(dialog, self.OnClick)
	def SetText(self, dialog):
		dialog.SetText(self.widgetName, text=self.title)

class PzrDirButton(PzrValueButton):
	def AskValue(self):
		folder = AskDirectory(dir=self.default, title="Select a directory...")
		if folder:
                        return folder
		else:
                        return self.null
	def setDefaults(self):
                PzrValueButton.setDefaults(self)
                self.defaultText = "Load"
                self.defaultValue = os.path.dirname(poser.AppLocation())

class PzrFileButton(PzrValueButton):
	def AskValue(self):
		file = AskOpen(extensions=self.defaultExtensions, dir=self.default, title=self.message)
		if file:
			return file
		else:
			return self.null
	def setDefaults(self):
                PzrValueButton.setDefaults(self)
        	self.defaultText = "Load"
        	self.defaultValue = os.path.dirname(poser.AppLocation())
        	self.defaultMessage = "Open..."
                self.defaultExtensions = (".pz3", ".pzz")

class PzrMenuButton(PzrValueButton):
	def GetOptions(self):
		return self.options
	def AskValue(self):
		item = AskMenu(message=self.message, options=self.GetOptions())
		if item:
                        return item
		else:
                        return self.null
	def setDefaults(self):
                PzrValueButton.setDefaults(self)
        	self.defaultText = "Choose"
                self.defaultValue = None
        	self.defaultMessage = "Select an item."
        	self.defaultOptions = []

class PzrFloatButton(PzrValueButton):
	def AskValue(self, event=None):
                value = AskText(self.message)
                if value:
                        # Check for European style decimals
                        value = value.replace(",",".")
                        return float(value)
                else:
                        return self.null
	def FormatText(self, text):
                s = repr(round(text, 6))
                if s.find(".") != -1:
                        while s[-1] == "0":
                                s = s[:-1]
                        s += "0"
                return PzrValueButton.FormatText(self, s)
	def FormatValue(self, value):
		return float(value)
	def setDefaults(self):
                PzrValueButton.setDefaults(self)
                self.defaultText = "0.0"
        	self.defaultValue = 0.0
        	self.defaultMessage = "Enter a number."

class PzrBoolButton(PzrValueButton):
        def __init__(self, widgetName, command=10002, groupID=4, text="!NULL", value="!NULL", message=None, trueText="On", falseText="Off"):
                self.trueText = trueText
                self.falseText = falseText
                PzrValueButton.__init__(self, widgetName, command=command, groupID=groupID, text=text, value=value, message=message)
        def AskValue(self, event=None):
                return poser.DialogSimple.YesNo(self.message)
        def FormatText(self, text):
                if text:
                        return self.trueText
                else:
                        return self.falseText
        def FormatValue(self, value):
                if value:
                        return 1
                else:
                        return 0
        def setDefaults(self):
                PzrValueButton.setDefaults(self)
                self.defaultText = "Off"
                self.defaultValue = 0
                self.defaultMessage = "On or off?"

class PzrIntButton(PzrValueButton):
	def AskValue(self, event=None):
                value = AskText(self.message)
                if value:
                        return int(value)
                else:
                        return self.null
	def FormatText(self, text):
		return PzrValueButton.FormatText(self, repr(text))
	def FormatValue(self, value):
		return int(value)
	def setDefaults(self):
                PzrValueButton.setDefaults(self)
        	self.defaultText = "0.0"
        	self.defaultValue = 0
        	self.defaultMessage = "Enter an integer."

class PzrPickImageButton(PzrValueButton):
	def AskValue(self, event=None):
		return poser.DialogSimple.PickImage(self.message)
	def FormatText(self, text):
		if text:
			return os.path.basename(text)
		else:
			return "None"
	def FormatValue(self, value):
		return value
	def setDefaults(self):
                PzrValueButton.setDefaults(self)
                self.defaultText = "None"
        	self.defaultValue = None

class PzrStringButton(PzrValueButton):
	def AskValue(self, event=None):
                value = AskText(message=self.message)
                if value:
                        return value
                else:
                        return self.null

def MessageBox(message="Text goes here"):
	poser.DialogSimple.MessageBox(message)

def ErrorBox(message="Error!"):
	MessageBox(message)

def AskText(message="\nEnter the name for\nthe new group."):
	txt = poser.DialogTextEntry(message=message)
	if txt.Show():
		return txt.Text()
	else:
		return 0

def AskDirectory(dir=os.path.dirname(poser.AppLocation()), title="Open...", parent=None):
	askdir = poser.DialogDirChooser(parentDialog=parent, message=title, startDir=dir)
	if askdir.Show():
		folder = askdir.Path()
		os.chdir(os.path.dirname(poser.AppLocation()))
		if not os.path.isdir(folder):
			return None
		else:
			return folder
	else:
		return None

def AskOpen(extensions=(".fx6",".FX6"),dir=os.path.dirname(poser.AppLocation()), title="Open...", parent=None):
	askfile = poser.DialogFileChooser(type=poser.kDialogFileChooserOpen, parentDialog=parent, message=title, startDir=dir)
	askfile.Show()
	file = askfile.Path()
	os.chdir(os.path.dirname(poser.AppLocation()))
	if not os.path.isfile(file):
		return None
	else:
		good = 0
		for ext in extensions:
			if not string.lower(file[-1 * len(ext):]) == string.lower(ext):
				continue
			else:
				good = 1
		if not good:
			error_string = """Bad file type. Must be """
			for ext in extensions:
				if (ext == extensions[-1]) and (len(extensions) > 1):
					thread = " or " + ext + "."
				elif len(extensions) > 1:
					thread = ext + ", "
				else:
					thread = ext + "."
				error_string = error_string +  thread
			ErrorBox(message=error_string)
			return AskOpen(extensions=extensions, dir=dir, title=title)
	return file

def AskSaveAs(extensions=(".txt",".TXT"),dir=os.path.dirname(poser.AppLocation()), title="Save as...", parent=None):
	askfile = poser.DialogFileChooser(type=poser.kDialogFileChooserSave, parentDialog=parent, message=title, startDir=dir)
	askfile.Show()
	file = askfile.Path()
	if os.path.isdir(file):
		file = None
	else:
		good = 0
		for ext in extensions:
                        if len(file) < len(ext):
                                continue
			elif not string.lower(file[-1 * len(ext):]) == ext:
				continue
			else:
				good = 1
				break
		if not good:
			file = file + extensions[0]
	os.chdir(os.path.dirname(poser.AppLocation()))
	return file

def AskMenu(title="Title", message="Select item", options=[]):
	if options == []:
		return None
	return poser.DialogSimple.AskMenu(title, message, options)

class PzrDialog(PzrGUIObject):
	def __init__(self, title="", message="", width=480, height=320, xmlpath="tempDlg.xml", borders=1, palettes=[]):
		self.widgets = []
		self.xmlpath = xmlpath
		self.title = title
		self.message = message
		self.width = width
		self.height = height
		if type(palettes) != type(list):
                        palettes = [palettes]
		self.xmlManager = PzrWidgetList( self.xmlpath, PSDFiles=(["./runtime/ui/26000_utility.psd"] + palettes), useBorders=borders )
		self.showing = 0
	def add(self, widget, x, y):
		self.widgets.append((widget, x, y))
	def AddButtonCallback(self, name=None, callback=None):
		self.dlg.AddButtonCallback(name=name, callback=callback)
	def SetButtonValue(self, name=None, value=0):
		self.dlg.SetButtonValue(name=name, value=value)
	def SetText(self, name=None, text=""):
		self.dlg.SetText(name=name, text=text)
	def Show(self):
		self.showing = 1
		for widget, x, y in self.widgets:
			self.xmlManager.addXML( widget.place(x,y) )
		self.xmlManager.close()
		self.dlg = poser.Dialog( self.xmlpath, title=self.title, message=self.message, width=self.width, height=self.height)
		for widget, x, y in self.widgets:
			widget.SetDialog( self.dlg )
		self.onShow()
		return self.dlg.Show()
	def Text(self):
		return self.dlg.Text()
	def get(self):
		return None
	def onShow(self):
		return None

BID_DRAG = 4

CMD_ACCEPT = 7001
CMD_REJECT = 7002

if __name__ == '__main__':
        pass
pass
